package com.qqtech.qquan.im.controller;

import java.util.Properties;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.util.HttpClientUtil;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.im.model.ImConfigBean;
import com.qqtech.qquan.im.model.ImUser;
import com.qqtech.qquan.im.model.ImUserQuery;
import com.qqtech.qquan.im.service.ImUserService;
import com.qqtech.web.context.BaseWebController;

import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = "/im/ryapi")
public class ImRyApiController extends BaseWebController<ImUser, ImUserQuery> {
	@Resource
	private ImUserService imUserService;

	@Resource
	private ImConfigBean imConfigBean;

	@Override
	protected BaseService<ImUser> getBaseService() {
		return imUserService;
	}

	/**
	 * 根据用户id、昵称和头像获取服务器的token(打开APP和登录都调用)
	 * 
	 * @param imUser
	 * @return
	 */
	@RequestMapping(value = "/getToken")
	@ResponseBody
	@VisitAnnt(type = RespEnum.LOGIN)
	public Result getToken(@RequestBody ImUser imUser) {
		Result result = new Result();
		if (imUser == null) {
			result.addErr("参数错误");
			return result;
		}
		if (imUser.getUserId() == null || imUser.getUserId().intValue() == 0) {
			result.addErr("用户id不能为空");
		}
		if (StringUtil.isBlank(imUser.getImNickName())) {
			result.addErr("昵称不能为空");
		}
		if (StringUtil.isBlank(imUser.getImgString())) {
			result.addErr("头像不能为空");
		}
		if (result.checkForm()) {
			String url = "http://api.cn.ronghub.com/user/getToken.json";
			StringBuffer params = new StringBuffer();
			params.append("userId=").append(imUser.getUserId()).append("&");
			params.append("name=").append(imUser.getImNickName()).append("&");
			params.append("portraitUri=").append(imUser.getFileDomain()).append(imUser.getImgString());
			String response = sendRyRequest(url, params.toString());
			if (StringUtil.isNotBlank(response)) {
				JSONObject json = JSONObject.fromObject(response);
				if (json != null && StringUtil.isNotBlank((String) json.get("token"))) {
					result.put("ryUserId", (String) json.get("userId"));
					result.put("ryToken", (String) json.get("token"));
					result.put("ryAppKey", imConfigBean.getRyAppKey());
					result.put("xiaomiAppId", imConfigBean.getXiaomiAppId());
					result.put("xiaomiAppKey", imConfigBean.getXiaomiAppKey());
					return result;
				}
			}
			result.addErr("融云服务端数据异常");
		}
		return result;
	}

	private String sendRyRequest(String url, String requestBody) {
		String AppKey = imConfigBean.getRyAppKey();
		String AppSecret = imConfigBean.getRyAppSecret();
		String Nonce = StringUtil.getRandomNum(8);
		String Timestamp = "" + TimeUtil.now().getTime();
		String decrypt = AppSecret + Nonce + Timestamp;
		String Signature = DigestUtils.sha1Hex(decrypt);
		Properties props = new Properties();
		props.put("App-Key", AppKey);
		props.put("Nonce", Nonce);
		props.put("Timestamp", Timestamp);
		props.put("Signature", Signature);
		return HttpClientUtil.send(url, "POST", requestBody, props);
	}
}
